﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeafHttp
{
    /// <summary>
    /// Http客户端
    /// </summary>
    public class HttpClient
    {
        List<IRequestInterceptor> _interceptors = new List<IRequestInterceptor>();

        /// <summary>
        /// 基础地址
        /// </summary>
        public string BaseUrl { get; set; }
        /// <summary>
        /// 默认的请求标头
        /// </summary>
        public Dictionary<string, string> DefaultHeadersCommon { get; set; }
        /// <summary>
        /// 当Method为Post时附加的请求标头
        /// </summary>
        public Dictionary<string, string> DefaultHeadersPost { get; set; }
        /// <summary>
        /// 默认的Cookie
        /// </summary>
        public List<HttpCookie> DefaultCookies { get; set; }
        /// <summary>
        /// 请求超时的毫秒数（默认5000）
        /// </summary>
        public int DefaultTimeout { get; set; }
        /// <summary>
        /// 拦截器
        /// </summary>
        public List<IRequestInterceptor> Interceptors { get { return _interceptors; } }

        /// <summary>
        /// 构造函数
        /// </summary>
        public HttpClient()
        {
            this.DefaultHeadersCommon = new Dictionary<string, string>();
            this.DefaultHeadersPost = new Dictionary<string, string>();
            this.DefaultCookies = new List<HttpCookie>();
            this.DefaultTimeout = 5000;
        }

        /// <summary>
        /// 增加拦截器
        /// </summary>
        /// <param name="interceptor"></param>
        public void AddInterceptor(IRequestInterceptor interceptor)
        {
            if (interceptor == null)
                throw new ArgumentNullException(nameof(interceptor));

            _interceptors.Add(interceptor);
        }

        /// <summary>
        /// 删除拦截器
        /// </summary>
        /// <param name="interceptor"></param>
        public void RemoveInterceptor(IRequestInterceptor interceptor)
        {
            if (interceptor == null)
                throw new ArgumentNullException(nameof(interceptor));

            _interceptors.Remove(interceptor);
        }

        /// <summary>
        /// 创建请求
        /// </summary>
        /// <param name="config">请求参数</param>
        /// <returns></returns>
        public IHttpRequest Request(RequestConfig config)
        {
            return new HttpRequest(this, config);
        }

        /// <summary>
        /// 创建Get请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="config">请求参数</param>
        public IHttpRequest Get(string url, RequestConfig config = null)
        {
            if (config == null)
            {
                config = new RequestConfig();
            }
            config.Method = HttpMethod.Get;
            config.Url = url;
            return Request(config);
        }

        /// <summary>
        /// 创建Post请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">发送数据</param>
        /// <param name="config">请求参数</param>
        public IHttpRequest Post(string url, object data = null, RequestConfig config = null)
        {
            if (config == null)
            {
                config = new RequestConfig();
            }
            config.Method = HttpMethod.Post;
            config.Url = url;
            config.Data = data;
            return Request(config);
        }

        /// <summary>
        /// 创建Put请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="data">发送数据</param>
        /// <param name="config">请求参数</param>
        public IHttpRequest Put(string url, object data = null, RequestConfig config = null)
        {
            if (config == null)
            {
                config = new RequestConfig();
            }
            config.Method = HttpMethod.Put;
            config.Url = url;
            config.Data = data;
            return Request(config);
        }

        /// <summary>
        /// 创建Delete请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="config">请求参数</param>
        public IHttpRequest Delete(string url, RequestConfig config = null)
        {
            if (config == null)
            {
                config = new RequestConfig();
            }
            config.Method = HttpMethod.Delete;
            config.Url = url;
            return Request(config);
        }
    }
}
